package com.typhoon.spring_shiro.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;


public final class RequestUtil {

	public static final String REQUEST_GET = "get";

	public static final String REQUEST_POST = "post";


	public static boolean isGet(HttpServletRequest request) {
		return REQUEST_GET.equalsIgnoreCase(request.getMethod());
	}

	public static boolean isPost(HttpServletRequest request) {
		return REQUEST_POST.equalsIgnoreCase(request.getMethod());
	}


	public static String getRemoteIp(HttpServletRequest request) {
		if (request == null) {
			return "";
		}

		String ipaddr = request.getHeader("Cdn-Src-Ip");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Real-IP");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Forwarded-For");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("X-Remote-Addr");
		if (StringUtils.isNotBlank(ipaddr)) {
			return ipaddr;
		}

		ipaddr = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (StringUtils.isBlank(ipaddr)) {
			ipaddr = request.getRemoteAddr();
		}
		return ipaddr;
	}


	public static String getTableName(String tableName, int tableAmounts, int id) {
		if (tableAmounts == 10) {
			return getTableName(tableName, tableAmounts, id, 2);
		}
		return getTableName(tableName, tableAmounts, id,
				String.valueOf(tableAmounts).length() - 1);
	}

	
	public static String getTableName(String tableName, int tableAmounts,
			int id, int bit) {
		return tableName + "_"
				+ String.format("%0" + bit + "d", id % tableAmounts, bit);
	}

	/**
	 * 
	 * @param id
	 * @param mod
	 * @return
	 */
	public static String format(int id, int mod) {
		return String.format("%0" + (String.valueOf(mod).length() - 1) + "d",
				id % mod);
	}

	public static String format(int id, int mod, int bit) {
		return String.format("%0" + bit + "d", id % mod);
	}


	public static <T> String getLoggerDes(String title, T... items) {
		StringBuilder des = new StringBuilder(title);
		des.append("|");
		for (Object object : items) {
			des.append(object).append("|");
		}
		return des.toString();
	}


	
	public static int getRandom(int start, int end)
	{
		if(start > end || start < 0 || end < 0)
		{
			return -1;
		}
		return (int)(Math.random() * (end - start + 1)) + start;
	}
	
	public static String getRandomString(int length) { 
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";     
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 }

	public static Map<String, String> getGame() {
		Map<String, String> map = new HashMap<String, String>();
//		map.put("lightPoint", "2");
		map.put("lightCount", "3");
//		map.put("lightTotalPoint", "10");
		map.put("totalPoint", "6");
		return map;
	}  
}